package com.wman.lib.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wman.sheep.common.base.BaseEvent;
import com.wman.sheep.common.utils.TLog;
import com.wman.sheep.common.utils.ToastUtil;
import com.wman.lib.AppConfig;

import org.greenrobot.eventbus.EventBus;

/**
 * Name     youcai
 * Anthor   wman
 * Time     2019/08/18 0018 23:28
 * Describe 描述
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    public int WX_LOGIN = 1;

    private IWXAPI iwxapi;

    private SendAuth.Resp resp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getSupportActionBar().hide();
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        iwxapi = WXAPIFactory.createWXAPI(this, AppConfig.WX_APPID, true);
        //接收到分享以及登录的intent传递handleIntent方法，处理结果
        iwxapi.handleIntent(getIntent(), this);
    }


    @Override
    public void onReq(BaseReq baseReq) {
    }


    //请求回调结果处理
    @Override
    public void onResp(BaseResp baseResp) {
        //微信登录为getType为1，分享为0
        if (baseResp.getType() == WX_LOGIN) {
            resp = (SendAuth.Resp) baseResp;
            TLog.d("登陆回调的结果:" + new Gson().toJson(resp));
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    postEvent(AppConfig.EVENT_WX_RESP_CODE, String.valueOf(resp.code));
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED://用户拒绝授权
                    ToastUtil.showTextToast("获取授权失败...");
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
                    ToastUtil.showTextToast("获取授权失败...");
                    break;
                default:
                    break;
            }
        } else if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
            String extraData = launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
            TLog.d("App调起小程序:" + new Gson().toJson(resp));
        }
        //https://mp.weixin.qq.com/s?__biz=MjM5NDAwMTA2MA==&mid=2695730124&idx=1&sn=666a448b047d657350de7684798f48d3&chksm=83d74a07b4a0c311569a748f4d11a5ebcce3ba8f6bd5a4b3183a4fea0b3442634a1c71d3cdd0&scene=21#wechat_redirect
        //6月份新版微信客户端发布后，用户从微信内的网页或第三方App，分享消息给微信好友，以及分享到朋友圈，开发者将无法获知用户是否分享完成。
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void postEvent(int eventCode, Object data) {
        BaseEvent event = new BaseEvent();
        event.setEventType(eventCode);
        event.setData(data);
        EventBus.getDefault().post(event);
    }
}
